{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #字符串中的单词反转"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reverseMessage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串中的单词反转"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你在与一位习惯从右往左阅读的朋友发消息，他发出的文字顺序都与正常相反但单词内容正确，为了和他顺利交流你决定写一个转换程序，把他所发的消息 <code>message</code> 转换为正常语序。</p>\n",
    "\n",
    "<p>注意：输入字符串 <code>message</code> 中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中，单词间应当仅用单个空格分隔，且不包含任何额外的空格。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> message = \"<code>the sky is blue</code>\"\n",
    "<strong>输出:&nbsp;</strong>\"<code>blue is sky the</code>\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> message = \" &nbsp;hello world! &nbsp;\"\n",
    "<strong>输出:&nbsp;</strong>\"world! hello\"\n",
    "<strong>解释: </strong>输入字符串可以在前面或者后面包含多余的空格，但是反转后的字符不能包括。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> message = \"a good &nbsp; example\"\n",
    "<strong>输出:&nbsp;</strong>\"example good a\"\n",
    "<strong>解释: </strong>如果两个单词间有多余的空格，将反转后单词间的空格减少到只含一个。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= message.length &lt;= 10^4</code></li>\n",
    "\t<li><code>message</code> 中包含英文大小写字母、空格和数字</li>\n",
    "\t<li><code>message</code> 中至少有一个单词</li>\n",
    "\t<li>&nbsp;</li>\n",
    "</ul>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>本题与主站 151 题相同：<a href=\"https://leetcode-cn.com/problems/reverse-words-in-a-string/\">https://leetcode-cn.com/problems/reverse-words-in-a-string/</a></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [fan-zhuan-dan-ci-shun-xu-lcof](https://leetcode.cn/problems/fan-zhuan-dan-ci-shun-xu-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [fan-zhuan-dan-ci-shun-xu-lcof](https://leetcode.cn/problems/fan-zhuan-dan-ci-shun-xu-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"the sky is blue\"', '\"  hello world!  \"', '\"a good   example\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=' '+message\n",
    "        q=''\n",
    "        i,j=len(message)-1,len(message)-1\n",
    "        while i>0:\n",
    "          while i>=0 and message[i]!=' ':\n",
    "            i-=1\n",
    "          q=q+message[i+1:j+1]+' '\n",
    "          while i>=0 and message[i]==' ':\n",
    "            i-=1\n",
    "          j=i\n",
    "        return q.strip()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message1 = message.strip()\n",
    "        record = ''\n",
    "        ans = ''\n",
    "        for i in message1:\n",
    "            if i != ' ':\n",
    "                record = record + i\n",
    "            elif i == ' ' and record != '':\n",
    "                ans = ' ' + record + ans\n",
    "                record = ''\n",
    "\n",
    "        ans = record + ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        return ans.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        m,k=\"\",[]\n",
    "        for i in message:\n",
    "            if i!=' ':m+=i\n",
    "            else:\n",
    "                if not m: continue\n",
    "                else:\n",
    "                    k.append(m)\n",
    "                    m=\"\"\n",
    "        if m:k.append(m)\n",
    "        m=\"\"\n",
    "        for i in k:\n",
    "            i=i[::-1]\n",
    "            m+=i\n",
    "            m+=\" \"\n",
    "        return m[-2::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, s: str) -> str:\n",
    "        s = s[::-1]\n",
    "        s = s.split()\n",
    "        for i in range(len(s)):\n",
    "             s[i] = s[i][::-1]\n",
    "        return ' '.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        new = message.split()\n",
    "        res = []\n",
    "        len1 = len(new)\n",
    "        for i in new[::-1]:\n",
    "            i.strip()\n",
    "            res.append(i)\n",
    "        return \" \".join(res).strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "\n",
    "        space_count = None\n",
    "        tmp = ''\n",
    "        res = ''\n",
    "        for i in message:\n",
    "            if space_count == None:\n",
    "                if i != ' ':\n",
    "                    space_count = 0\n",
    "                    tmp += i\n",
    "\n",
    "            elif space_count == 0:\n",
    "                if i != ' ':\n",
    "                    tmp += i\n",
    "                else:\n",
    "                    space_count += 1\n",
    "                    if res == '':\n",
    "                        res = tmp\n",
    "                    else:\n",
    "                        res = tmp + ' ' + res\n",
    "                    tmp = ''\n",
    "            else:\n",
    "                if i != ' ':\n",
    "                    space_count = 0\n",
    "                    tmp += i\n",
    "                else:\n",
    "                    space_count += 1\n",
    "\n",
    "        if tmp != '':\n",
    "            if res != '':\n",
    "                res = tmp + ' ' + res\n",
    "            else:\n",
    "                res = tmp + res\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        res=[]\n",
    "        message = message.strip()\n",
    "        i = j = len(message)-1\n",
    "\n",
    "        while i>=0:\n",
    "            while i>=0 and message[i]!= ' ':i-=1\n",
    "            res.append(message[i+1:j+1])\n",
    "            while message[i]==' ':i-=1\n",
    "            j=i\n",
    "        return \" \".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "\n",
    "        space_count = None\n",
    "        tmp = ''\n",
    "        res = ''\n",
    "        for i in message:\n",
    "            if space_count == None:\n",
    "                if i != ' ':\n",
    "                    space_count = 0\n",
    "                    tmp += i\n",
    "\n",
    "            elif space_count == 0:\n",
    "                if i != ' ':\n",
    "                    tmp += i\n",
    "                else:\n",
    "                    space_count += 1\n",
    "                    if res == '':\n",
    "                        res = tmp\n",
    "                    else:\n",
    "                        res = tmp + ' ' + res\n",
    "                    tmp = ''\n",
    "            else:\n",
    "                if i != ' ':\n",
    "                    space_count = 0\n",
    "                    tmp += i\n",
    "                else:\n",
    "                    space_count += 1\n",
    "\n",
    "        if tmp != '':\n",
    "            if res != '':\n",
    "                res = tmp + ' ' + res\n",
    "            else:\n",
    "                res = tmp + res\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "\n",
    "        space_count = None\n",
    "        tmp = ''\n",
    "        res = ''\n",
    "        for i in message:\n",
    "            if space_count == None:\n",
    "                if i != ' ':\n",
    "                    space_count = 0\n",
    "                    tmp += i\n",
    "\n",
    "            elif space_count == 0:\n",
    "                if i != ' ':\n",
    "                    tmp += i\n",
    "                else:\n",
    "                    space_count += 1\n",
    "                    if res == '':\n",
    "                        res = tmp\n",
    "                    else:\n",
    "                        res = tmp + ' ' + res\n",
    "                    tmp = ''\n",
    "            else:\n",
    "                if i != ' ':\n",
    "                    space_count = 0\n",
    "                    tmp += i\n",
    "                else:\n",
    "                    space_count += 1\n",
    "\n",
    "        if tmp != '':\n",
    "            if res != '':\n",
    "                res = tmp + ' ' + res\n",
    "            else:\n",
    "                res = tmp + res\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        i, j, res = len(message)-1, len(message)-1, []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != \" \": i -= 1\n",
    "            res.append(message[i+1:j+1])\n",
    "            while i >= 0 and message[i] == \" \": i -= 1\n",
    "            j = i \n",
    "        return \" \".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()   # 删除首尾空格\n",
    "        strs = message.split()      # 分割字符串\n",
    "        strs.reverse()              # 翻转单词列表\n",
    "        return ' '.join(strs)       # 拼接为字符串并返回\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return \" \".join(reversed(message.split()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        wordset = message.split()\n",
    "        wordset.reverse()\n",
    "        return \" \".join(wordset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        res=[]\n",
    "        res=message.strip().split()\n",
    "\n",
    "        i,j=0,len(res)-1\n",
    "        while i<j:\n",
    "            res[i],res[j]=res[j],res[i]\n",
    "            i+=1\n",
    "            j-=1\n",
    "        return \" \".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        a=len(message)\n",
    "        c=\"\"\n",
    "        b=\"\"\n",
    "        n=0\n",
    "        for i in range(a-1,-1,-1):\n",
    "            if message[i]!=' ':\n",
    "                b=b+message[i]\n",
    "            if i>0:\n",
    "                if message[i]==' ':\n",
    "                    if message[i-1]!=' ':\n",
    "                        b=b[::-1]\n",
    "                        if b!=\"\":\n",
    "                            c=c+b+' '\n",
    "                        b=\"\"\n",
    "                    elif message[i-1]==' ':\n",
    "                        b=b\n",
    "        b=b[::-1]\n",
    "        c=c+b\n",
    "        return c\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=message.strip()\n",
    "        newCells=message.split()\n",
    "        newCells.reverse()\n",
    "        return \" \".join(newCells)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.split()\n",
    "        ans = \"\"\n",
    "        for i in range(len(message)-1, -1, -1):\n",
    "            ans += ' '\n",
    "            ans += message[i]\n",
    "        return ans[1:]\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        a=len(message)\n",
    "        c=\"\"\n",
    "        b=\"\"\n",
    "        n=0\n",
    "        for i in range(a-1,-1,-1):\n",
    "            if message[i]!=' ':\n",
    "                b=b+message[i]\n",
    "            if i>0:\n",
    "                if message[i]==' ':\n",
    "                    if message[i-1]!=' ':\n",
    "                        b=b[::-1]\n",
    "                        if b!=\"\":\n",
    "                            c=c+b+' '\n",
    "                        b=\"\"\n",
    "                    elif message[i-1]==' ':\n",
    "                        b=b\n",
    "        b=b[::-1]\n",
    "        if b!=\"\":\n",
    "            c=c+b\n",
    "        return c\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=message.strip()\n",
    "        res=message.split()\n",
    "        res.reverse()\n",
    "        print(res)\n",
    "        return \" \".join(res)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()                      # 删除首尾空格\n",
    "        i = j = len(message) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ': i -= 1 # 搜索首个空格\n",
    "            res.append(message[i + 1: j + 1])          # 添加单词\n",
    "            while i >= 0 and message[i] == ' ': i -= 1 # 跳过单词间空格\n",
    "            j = i                                      # j 指向下个单词的尾字符\n",
    "        return ' '.join(res)                           # 拼接并返回\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()                      # 删除首尾空格\n",
    "        i = j = len(message) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ': i -= 1 # 搜索首个空格\n",
    "            res.append(message[i + 1: j + 1])          # 添加单词\n",
    "            while i >= 0 and message[i] == ' ': i -= 1 # 跳过单词间空格\n",
    "            j = i                                      # j 指向下个单词的尾字符\n",
    "        return ' '.join(res)                           # 拼接并返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        i = j = len(message) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ': i -= 1\n",
    "            res.append(message[i + 1: j + 1])\n",
    "            while i >= 0 and message[i] == ' ': i -= 1\n",
    "            j = i      \n",
    "        return ' '.join(res)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        i = j = len(message) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ':\n",
    "                i -= 1\n",
    "            res.append(message[i + 1 : j + 1])\n",
    "            while i >= 0 and message[i] == ' ':\n",
    "                i -= 1\n",
    "            j = i\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        s=message.strip()\n",
    "        s=s.split(\" \")\n",
    "        l=[]\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i] :\n",
    "                s[i]=s[i].strip()\n",
    "                l.append(s[i])\n",
    "        return \" \".join(l).strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        t = message.split(' ')\n",
    "        return ' '.join([i for i in t[::-1] if i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        a=len(message)\n",
    "        c=\"\"\n",
    "        b=\"\"\n",
    "        n=0\n",
    "        for i in range(a-1,-1,-1):\n",
    "            if message[i]!=' ':\n",
    "                b=b+message[i]\n",
    "            if i>0:\n",
    "                if message[i]==' ':\n",
    "                    if message[i-1]!=' ':\n",
    "                        b=b[::-1]\n",
    "                        if b!=\"\":\n",
    "                            c=c+b+' '\n",
    "                        b=\"\"\n",
    "        b=b[::-1]\n",
    "        c=c+b\n",
    "        return c\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, s: str) -> str:\n",
    "        s=s+' '\n",
    "        a=\"\"\n",
    "        c=0\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "          if s[i]==' ' and s[i-1]!=' ':\n",
    "            c=i-1\n",
    "          if s[i]!=' ' and s[i-1]==' ':\n",
    "           while s[i]!=' ':\n",
    "            a=a+s[i]\n",
    "            if i==c:\n",
    "                a=a+' '\n",
    "            i=i+1\n",
    "        a=a[0:len(a)-1]\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        res = []\n",
    "        message = message.strip()\n",
    "        i=j=len(message) - 1\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ':\n",
    "                i -= 1\n",
    "            res.append(message[i + 1:j + 1])\n",
    "            while i >= 0 and message[i] == ' ':\n",
    "                i -= 1\n",
    "            j = i\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        '''\n",
    "        tmp_list = [' ']\n",
    "        for i in range(len(message)):\n",
    "            tmp_list.append(message[i])\n",
    "        tmp_list.append(' ')\n",
    "        word_head_list = []\n",
    "        word_end_list = []\n",
    "        for m in range(len(tmp_list)-1):\n",
    "            if (tmp_list[m] == ' ') and  (tmp_list[m+1] != ' '):\n",
    "                word_head_list.append(m+1)\n",
    "            elif (tmp_list[m] != ' ') and  (tmp_list[m+1] == ' '):\n",
    "                word_end_list.append(m+1)\n",
    "        if not  word_head_list:\n",
    "            return ''\n",
    "        sen_list  = []\n",
    "        for i in range(len(word_head_list)-1, -1, -1):\n",
    "            sen_list.append(''.join(tmp_list[word_head_list[i]: word_end_list[i]]))\n",
    "            sen_list.append(' ')\n",
    "        sen_list.pop()\n",
    "        a = \"\".join(sen_list)\n",
    "        return a\n",
    "        '''\n",
    "        m1 = message.strip()\n",
    "        m2 = m1.split()\n",
    "        m3 = m2[::-1]\n",
    "        m4 = ' '.join(m3)\n",
    "        return m4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        res = message.split()\n",
    "        return ' '.join(reversed(res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        word_list = []\n",
    "        word_start, word_end = False, False\n",
    "        word = \"\"\n",
    "        for index, i in enumerate(message):\n",
    "            if i != \" \":\n",
    "                if not word_start:\n",
    "                    word_start = True\n",
    "                    word += i\n",
    "                elif not word_end:\n",
    "                    word += i\n",
    "\n",
    "                if index == len(message) - 1:\n",
    "                    word_end = True\n",
    "                    word_list.append(word)\n",
    "                    break\n",
    "\n",
    "            elif word_start:\n",
    "                word_end = True\n",
    "                word_list.append(word)\n",
    "                word = \"\"\n",
    "                word_start, word_end = False, False\n",
    "        return \" \".join(word_list[::-1])\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        items = message.split(' ')\n",
    "        new_list=[]\n",
    "        for item in items:\n",
    "            if item=='':\n",
    "                continue\n",
    "    \n",
    "            item=item.strip()\n",
    "            new_list.append(item)\n",
    "        s=''\n",
    "        l=len(new_list)\n",
    "        for i in range(len(new_list)):\n",
    "            s+=new_list[l-1-i]\n",
    "            s+=' '\n",
    "        s=s[:-1]\n",
    "        return s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        strs=message.strip().split(\" \")\n",
    "        out_array=[]\n",
    "        for item in strs:\n",
    "            if len(item)>0:\n",
    "                out_array.append(item)\n",
    "        out_str=\" \".join(out_array[::-1]) \n",
    "        return out_str\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return ' '.join([i for i in message.split(' ') if i!=''][::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message:str) -> str:\n",
    "        return \" \".join(message.split()[-1::-1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()                      # 删除首尾空格\n",
    "        i = j = len(message) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ': i -= 1 # 搜索首个空格\n",
    "            res.append(message[i + 1: j + 1])          # 添加单词\n",
    "            while i >= 0 and message[i] == ' ': i -= 1 # 跳过单词间空格\n",
    "            j = i                                      # j 指向下个单词的尾字符\n",
    "        return ' '.join(res) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        items = message.split(' ')\n",
    "        new_list=[]\n",
    "        for item in items:\n",
    "            if item=='':\n",
    "                continue\n",
    "    \n",
    "            item=item.strip()\n",
    "            new_list.append(item)\n",
    "        s=''\n",
    "        l=len(new_list)\n",
    "        for i in range(len(new_list)):\n",
    "            s+=new_list[l-1-i]\n",
    "            s+=' '\n",
    "        s=s[:-1]\n",
    "        return s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        tokens = message.split()\n",
    "        tokens.reverse()\n",
    "        return ' '.join(tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        s = message.split()\n",
    "        s.reverse()\n",
    "        s1 = ' '.join(s)\n",
    "        return s1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        i = j = len(message) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i] != ' ':\n",
    "                i -= 1\n",
    "            res.append(message[i + 1:j + 1])\n",
    "            while i >= 0  and message[i] == ' ':\n",
    "                i -= 1\n",
    "            j = i\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        n = len(message)-1\n",
    "        i=j=n\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            while i >= 0 and message[i]!=' ':i-=1\n",
    "            res.append(message[i+1:j+1])\n",
    "            while i >= 0 and message[i]==' ':i-=1\n",
    "            j = i\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        strs = message.split()\n",
    "        strs.reverse()\n",
    "        return \" \".join(strs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        t = message.split()\n",
    "        # print(t)\n",
    "        ans = \"\"\n",
    "        for i in t:\n",
    "            ans = i + \" \"+ ans\n",
    "        return ans[0:-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return \" \".join(message.split()[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=message.strip()\n",
    "        strs=message.split()\n",
    "        strs.reverse()\n",
    "        return \" \".join(strs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return ' '.join(message.split()[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        res = []\n",
    "        message = message.strip()\n",
    "        end,start = len(message)-1,len(message)-1\n",
    "        while start>=0:\n",
    "            while message[start]!=' ' and start >= 0:start -= 1\n",
    "            res.append(message[start+1:end+1])\n",
    "            while message[start]==' ':start -= 1\n",
    "            end = start\n",
    "        return ' '.join(res)\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        words = message.split()\n",
    "        words.reverse()\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        sen = message.split(\" \")\n",
    "        sen = [s for s in sen if s]\n",
    "        sen.reverse()\n",
    "        return \" \".join(sen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=message.strip()\n",
    "        i=j=len(message)-1\n",
    "        res=[]\n",
    "        while i>-1:\n",
    "            while message[i]!=\" \" and i>-1:i-=1\n",
    "            res.append(message[i+1:j+1])\n",
    "            while message[i]==\" \" and i>-1:i-=1\n",
    "            j=i\n",
    "        return \" \".join(res)\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return \" \".join(message.strip().split()[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=message.split()[::-1]\n",
    "        return ' '.join(message)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        strs = message.strip().split()      \n",
    "        strs.reverse()              \n",
    "        return ' '.join(strs) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        s = message.split()\n",
    "        s = s[0:len(s)]\n",
    "        res = \"\"\n",
    "        for i in range(len(s)):\n",
    "            res += s[len(s) - i - 1]\n",
    "            if i < len(s) - 1:\n",
    "                res += \" \"\n",
    "        # res_2 = \"\"\n",
    "        # for i in range(len(res)):\n",
    "        #     if i < len(res) - 1:\n",
    "        #         if res[i] == ' ' and res[i + 1] == ' ':\n",
    "        #             continue\n",
    "        #     res_2 += res[i]\n",
    "        return res\n",
    "        # return ' '.join(message.strip().split()[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message = message.strip()\n",
    "        strs = message.split()\n",
    "        strs.reverse()\n",
    "        return ' '.join(strs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return ' '.join(message.strip().split()[::-1])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "\n",
    "        str_l = message.split()\n",
    "        if len(str_l) == 0: return \"\"\n",
    "        ret = str_l[-1]\n",
    "        for i in range(len(str_l) - 1):\n",
    "            if \"\" != str_l[len(str_l) - 2 - i]:\n",
    "                ret += \" \" + str_l[len(str_l) - 2 - i]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        return ' '.join(message.strip().split()[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        message=message.strip()\n",
    "        i=j=len(message)-1\n",
    "        res=[]\n",
    "        while i>=0:\n",
    "            while i>=0 and message[i]!=' ': i-=1\n",
    "            res.append(message[i+1:j+1])\n",
    "            while i>=0 and message[i]==' ':i-=1\n",
    "            j=i\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        a = message.strip().split(\" \")\n",
    "        b = []\n",
    "        for i in range(len(a) - 1, -1, -1):\n",
    "            if a[i]:\n",
    "                b.append(a[i])\n",
    "            continue\n",
    "        return \" \".join(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "       words = message.split()#分割成单词列表\n",
    "       words.reverse()#单词反转\n",
    "       new_message = ' '.join(words)#加入分割空格符成为反转字符串\n",
    "       return new_message\n",
    "       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseMessage(self, message: str) -> str:\n",
    "        sen = message.split(\" \")\n",
    "        sen = [s for s in sen if s]\n",
    "        sen.reverse()\n",
    "        return \" \".join(sen)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
